#include "fontandback.h"
#include"ui_font_back.h"
#include<qdebug.h>
#include<QFileDialog>
fontandback::fontandback(QWidget *parent) : QDialog(parent), uifont(new Ui::Form)
{

    uifont->setupUi(this);
    this->setWindowTitle("设置字体");
    this->setStyleSheet(QString::fromUtf8("#Form{border-image:url(:/new/prefix2/3.png)}"));
    //运行次数:
    this->yunxinnum=0;

    QSqlDatabase mysql;
    if(QSqlDatabase::contains("qt_sql_default_connection"))
    mysql = QSqlDatabase::database("qt_sql_default_connection");
  else
    mysql = QSqlDatabase::addDatabase("QSQLITE");
    mysql.setDatabaseName("../myshujuku.db");
  //打开数据库
  if(mysql.open()==true)
  {
      //开启事务
      QSqlDatabase::database().transaction();
      //sql语句,载入fonkandback
      QSqlQuery query;//不可以做成员,会报错
      query.exec("select * from fontandback");
      if(query.first()==true)
      {

          this->qttomp4font=query.value(7).toString();qDebug()<<"第一次从数据库载入数据fontandback"<<this->qttomp4font;
          this->filename_back=query.value(8).toString();qDebug()<<filename_back;
          this->filename_logo=query.value(9).toString();qDebug()<<filename_logo;
          this->rgb_r=query.value(1).toInt();qDebug()<<rgb_r;
          this->rgb_g=query.value(2).toInt();qDebug()<<rgb_g;
          this->rgb_b=query.value(3).toInt();qDebug()<<rgb_b;
          this->rgb_r_lo=query.value(4).toInt();qDebug()<<rgb_r_lo;
          this->rgb_g_lo=query.value(5).toInt();qDebug()<<rgb_g_lo;
          this->rgb_b_lo=query.value(6).toInt();qDebug()<<rgb_b_lo;
      }
      else
      {
          qDebug()<<"读取数据库失败fontandback";
          //关闭
          mysql.close();
      }
      qDebug()<<"(QString)rgb_r"<<QString::number( rgb_r);
      //初始化界面数值
      uifont->lineEdit_rgb1->setText(QString::number( rgb_r));
      uifont->lineEdit_rgb2->setText(QString::number( rgb_g));
      uifont->lineEdit_rgb3->setText(QString::number( rgb_b));
      uifont->lineEdit_zhi1->setText(QString::number( rgb_r_lo));
      uifont->lineEdit_zhi2->setText(QString::number( rgb_g_lo));
      uifont->lineEdit_zhi3->setText(QString::number( rgb_b_lo));

      mysql.close();
  }
  else
  {
      QMessageBox::warning(this,"未能打开数据库!",mysql.lastError().text());
      //结束程序:
  }
}

fontandback::~fontandback()
{
    delete uifont;

}

//槽函数:
//更改数据库:
void fontandback::on_comboBox_activated(const QString &arg1)
{

    QSqlDatabase mysql;
    if(QSqlDatabase::contains("qt_sql_default_connection"))
    mysql = QSqlDatabase::database("qt_sql_default_connection");
  else
    mysql = QSqlDatabase::addDatabase("QSQLITE");
    mysql.setDatabaseName("../myshujuku.db");
    mysql.open();
    //开启事务
    QSqlDatabase::database().transaction();
    //sql
    QSqlQuery query;//不可以做成员,会报错
    //删除原有数据:
    this->exectext=QString("delete from fontandback where font ='%1'").arg(this->qttomp4font);
    query.exec(this->exectext);
    //确定删除
    QSqlDatabase::database().commit();
    //更新元数据
    this->qttomp4font=arg1;
    //插入数据
    query.exec("create table fontandback(id int primary key, rgbr int, rgbg int, rgbb int, rgb_zhir int, rgb_zhig int, rgb_zhib int, font varchar(20), backfilename varchar(500), logofilename varchar(500))");
    this->exectext=QString("insert into fontandback(id,rgbr,rgbg,rgbb,rgb_zhir,rgb_zhig,rgb_zhib,font,backfilename,logofilename) values(1,%1,%2,%3,%4,%5,%6,'%7','%8','%9')").arg(this->rgb_r).arg(this->rgb_g).arg(this->rgb_b).arg(this->rgb_r_lo).arg(rgb_g_lo).arg(rgb_b_lo).arg(this->qttomp4font).arg(filename_back).arg(filename_logo);
    qDebug()<<"fontback84"<<this->exectext;
    query.exec(this->exectext);
    //确定
    QSqlDatabase::database().commit();
    //qDebug()<<"fontandback更改数据this.font"<<this->qttomp4font;
    mysql.close();
}

void fontandback::on_pushButton_backfilebname_clicked()
{
    QString temp=QFileDialog::getOpenFileName(this,"选择背景图片","",tr("Images (*.png *.xpm *.jpg)"));
    if(temp==nullptr)
    {
        return;
    }

    QSqlDatabase mysql;
    if(QSqlDatabase::contains("qt_sql_default_connection"))
    mysql = QSqlDatabase::database("qt_sql_default_connection");
  else
    mysql = QSqlDatabase::addDatabase("QSQLITE");
    mysql.setDatabaseName("../myshujuku.db");
    mysql.open();
    //开启事务
    QSqlDatabase::database().transaction();
    //sql
    QSqlQuery query;//不可以做成员,会报错
    //删除原有数据
    this->exectext=QString("delete from fontandback where id =1");
    query.exec(this->exectext);
    //确定
    QSqlDatabase::database().commit();
    //filename_back更新
    this->filename_back=temp;
    uifont->label_zhungtai->setText(this->filename_back);
    //插入
    query.exec("create table fontandback(id int primary key, rgbr int, rgbg int, rgbb int, rgb_zhir int, rgb_zhig int, rgb_zhib int, font varchar(20), backfilename varchar(500), logofilename varchar(500))");
    this->exectext=QString("insert into fontandback(id,rgbr,rgbg,rgbb,rgb_zhir,rgb_zhig,rgb_zhib,font,backfilename,logofilename) values(1,%1,%2,%3,%4,%5,%6,'%7','%8','%9')").arg(this->rgb_r).arg(this->rgb_g).arg(this->rgb_b).arg(this->rgb_r_lo).arg(rgb_g_lo).arg(rgb_b_lo).arg(this->qttomp4font).arg(filename_back).arg(filename_logo);
    query.exec(this->exectext);
    qDebug()<<"fontback"<<this->exectext;
    //确定
    QSqlDatabase::database().commit();
    mysql.close();

}

void fontandback::on_pushButton_logofilename_clicked()
{
    QSqlDatabase mysql;
    if(QSqlDatabase::contains("qt_sql_default_connection"))
    mysql = QSqlDatabase::database("qt_sql_default_connection");
  else
    mysql = QSqlDatabase::addDatabase("QSQLITE");
    mysql.setDatabaseName("../myshujuku.db");
    mysql.open();
    //开启事务
    QSqlDatabase::database().transaction();
    //sql
    QSqlQuery query;//不可以做成员,会报错
    //背景图默认无,仅在本次重写运行设置时有:主窗口析构时删除
    //第一次无删除

        //删除原有数据
        this->exectext=QString("delete from fontandback where logofilename ='%1'").arg(this->filename_logo);
        query.exec(this->exectext);
        //确定
        QSqlDatabase::database().commit();
    //更新
    this->filename_logo=QFileDialog::getOpenFileName(this,"选择logo图","",tr("Images (*.png *.xpm *.jpg)"));
    uifont->label_zhungtai->setText(this->filename_logo);

    //插入
    query.exec("create table fontandback(id int primary key, rgbr int, rgbg int, rgbb int, rgb_zhir int, rgb_zhig int, rgb_zhib int, font varchar(20), backfilename varchar(500), logofilename varchar(500))");
    this->exectext=QString("insert into fontandback(id,rgbr,rgbg,rgbb,rgb_zhir,rgb_zhig,rgb_zhib,font,backfilename,logofilename) values(1,%1,%2,%3,%4,%5,%6,'%7','%8','%9')").arg(this->rgb_r).arg(this->rgb_g).arg(this->rgb_b).arg(this->rgb_r_lo).arg(rgb_g_lo).arg(rgb_b_lo).arg(this->qttomp4font).arg(filename_back).arg(filename_logo);
    query.exec(this->exectext);qDebug()<<"fontback"<<this->exectext;
    //确定
    QSqlDatabase::database().commit();
    mysql.close();

}

//int型数据无''
//注意toint
void fontandback::on_lineEdit_rgb1_textChanged(const QString &arg1)
{
    if(this->yunxinnum<6)
    {
        yunxinnum++;
        qDebug()<<yunxinnum;
        return;
    }

    if(arg1.toInt()<-1|arg1.toInt()>255)
    {
        uifont->label_zhungtai->setText("rgb数值在0到255之间");
        //不插入数据的话,元数据不因该删除,this.rgb_r不因该改变
    }
    else
    {
        QSqlDatabase mysql;
        if(QSqlDatabase::contains("qt_sql_default_connection"))
        mysql = QSqlDatabase::database("qt_sql_default_connection");
      else
        mysql = QSqlDatabase::addDatabase("QSQLITE");
        mysql.setDatabaseName("../myshujuku.db");
        mysql.open();
        //开启事务
        QSqlDatabase::database().transaction();
        //sql
        QSqlQuery query;//不可以做成员,会报错
        //删除原有数据:
        this->exectext=QString("delete from fontandback where rgbr =%1").arg(this->rgb_r);
        query.exec(this->exectext);
        //确定
        QSqlDatabase::database().commit();
        //更新元数据
        this->rgb_r=arg1.toInt();
        qDebug()<<"this->rgb_r=arg1.toInt()"<<rgb_r;
        //插入数据
        query.exec("create table fontandback(id int primary key, rgbr int, rgbg int, rgbb int, rgb_zhir int, rgb_zhig int, rgb_zhib int, font varchar(20), backfilename varchar(500), logofilename varchar(500))");
        this->exectext=QString("insert into fontandback(id,rgbr,rgbg,rgbb,rgb_zhir,rgb_zhig,rgb_zhib,font,backfilename,logofilename) values(1,%1,%2,%3,%4,%5,%6,'%7','%8','%9')").arg(this->rgb_r).arg(this->rgb_g).arg(this->rgb_b).arg(this->rgb_r_lo).arg(rgb_g_lo).arg(rgb_b_lo).arg(this->qttomp4font).arg(filename_back).arg(filename_logo);
        query.exec(this->exectext);qDebug()<<"fontback"<<this->exectext;
        //确定
        QSqlDatabase::database().commit();
        mysql.close();

    }
}

void fontandback::on_lineEdit_rgb2_textChanged(const QString &arg1)
{
//初始化界面数值
    if(this->yunxinnum<6)
    {
        yunxinnum++;
        qDebug()<<yunxinnum;
        return;
    }
    if(arg1.toInt()<-1|arg1.toInt()>255)
    {
        uifont->label_zhungtai->setText("rgb数值在0到255之间");
    }
    else
    {
        QSqlDatabase mysql;
        if(QSqlDatabase::contains("qt_sql_default_connection"))
        mysql = QSqlDatabase::database("qt_sql_default_connection");
      else
        mysql = QSqlDatabase::addDatabase("QSQLITE");
        mysql.setDatabaseName("../myshujuku.db");
        mysql.open();
        //开启事务
        QSqlDatabase::database().transaction();
        //sql
        QSqlQuery query;//不可以做成员,会报错
        //删除原有数据:
        this->exectext=QString("delete from fontandback where rgbg =%1").arg(this->rgb_g);
        query.exec(this->exectext);
        //确定
        QSqlDatabase::database().commit();
        //更新元数据
        this->rgb_g=arg1.toInt();
        //插入数据
        query.exec("create table fontandback(id int primary key, rgbr int, rgbg int, rgbb int, rgb_zhir int, rgb_zhig int, rgb_zhib int, font varchar(20), backfilename varchar(500), logofilename varchar(500))");
        this->exectext=QString("insert into fontandback(id,rgbr,rgbg,rgbb,rgb_zhir,rgb_zhig,rgb_zhib,font,backfilename,logofilename) values(1,%1,%2,%3,%4,%5,%6,'%7','%8','%9')").arg(this->rgb_r).arg(this->rgb_g).arg(this->rgb_b).arg(this->rgb_r_lo).arg(rgb_g_lo).arg(rgb_b_lo).arg(this->qttomp4font).arg(filename_back).arg(filename_logo);
        query.exec(this->exectext);qDebug()<<"fontback"<<this->exectext;
        //确定
        QSqlDatabase::database().commit();
        mysql.close();

    }
}

void fontandback::on_lineEdit_rgb3_textChanged(const QString &arg1)
{

    if(this->yunxinnum<6)
    {
        yunxinnum++;
        qDebug()<<yunxinnum;
        return;
    }
    if(arg1.toInt()<-1|arg1.toInt()>255)
    {
        uifont->label_zhungtai->setText("rgb数值在0到255之间");
    }
    else
    {
        QSqlDatabase mysql;
        if(QSqlDatabase::contains("qt_sql_default_connection"))
        mysql = QSqlDatabase::database("qt_sql_default_connection");
      else
        mysql = QSqlDatabase::addDatabase("QSQLITE");
        mysql.setDatabaseName("../myshujuku.db");
        mysql.open();
        //开启事务
        QSqlDatabase::database().transaction();
        //sql
        QSqlQuery query;//不可以做成员,会报错
        //删除原有数据:
        this->exectext=QString("delete from fontandback where rgbb =%1").arg(this->rgb_b);
        query.exec(this->exectext);
        //确定
        QSqlDatabase::database().commit();
        //更新元数据
        this->rgb_b=arg1.toInt();
        //插入数据
        query.exec("create table fontandback(id int primary key, rgbr int, rgbg int, rgbb int, rgb_zhir int, rgb_zhig int, rgb_zhib int, font varchar(20), backfilename varchar(500), logofilename varchar(500))");
        this->exectext=QString("insert into fontandback(id,rgbr,rgbg,rgbb,rgb_zhir,rgb_zhig,rgb_zhib,font,backfilename,logofilename) values(1,%1,%2,%3,%4,%5,%6,'%7','%8','%9')").arg(this->rgb_r).arg(this->rgb_g).arg(this->rgb_b).arg(this->rgb_r_lo).arg(rgb_g_lo).arg(rgb_b_lo).arg(this->qttomp4font).arg(filename_back).arg(filename_logo);
        query.exec(this->exectext);qDebug()<<"fontback"<<this->exectext;
        //确定
        QSqlDatabase::database().commit();
        mysql.close();

    }
}

void fontandback::on_lineEdit_zhi1_textChanged(const QString &arg1)
{

    if(this->yunxinnum<6)
    {
        yunxinnum++;
        qDebug()<<yunxinnum;
        return;
    }
    if(arg1.toInt()<0|arg1.toInt()>255)
    {
        uifont->label_zhungtai->setText("rgb数值在0到255之间");
    }
    else
    {
        QSqlDatabase mysql;
        if(QSqlDatabase::contains("qt_sql_default_connection"))
        mysql = QSqlDatabase::database("qt_sql_default_connection");
      else
        mysql = QSqlDatabase::addDatabase("QSQLITE");
        mysql.setDatabaseName("../myshujuku.db");
        mysql.open();
        //开启事务
        QSqlDatabase::database().transaction();
        //sql
        QSqlQuery query;//不可以做成员,会报错
        //删除原有数据:
        this->exectext=QString("delete from fontandback where rgb_zhir =%1").arg(this->rgb_r_lo);
        query.exec(this->exectext);
        //确定
        QSqlDatabase::database().commit();

        //更新元数据
        this->rgb_r_lo=arg1.toInt();
        //插入数据
        query.exec("create table fontandback(id int primary key, rgbr int, rgbg int, rgbb int, rgb_zhir int, rgb_zhig int, rgb_zhib int, font varchar(20), backfilename varchar(500), logofilename varchar(500))");
        this->exectext=QString("insert into fontandback(id,rgbr,rgbg,rgbb,rgb_zhir,rgb_zhig,rgb_zhib,font,backfilename,logofilename) values(1,%1,%2,%3,%4,%5,%6,'%7','%8','%9')").arg(this->rgb_r).arg(this->rgb_g).arg(this->rgb_b).arg(this->rgb_r_lo).arg(rgb_g_lo).arg(rgb_b_lo).arg(this->qttomp4font).arg(filename_back).arg(filename_logo);
        query.exec(this->exectext);qDebug()<<"fontback"<<this->exectext;
        //确定
        QSqlDatabase::database().commit();
        mysql.close();

    }
}

void fontandback::on_lineEdit_zhi2_textChanged(const QString &arg1)
{

    if(this->yunxinnum<6)
    {
        yunxinnum++;
        qDebug()<<yunxinnum;
        return;
    }
    if(arg1.toInt()<0|arg1.toInt()>255)
    {
        uifont->label_zhungtai->setText("rgb数值在0到255之间");
    }
    else
    {
        QSqlDatabase mysql;
        if(QSqlDatabase::contains("qt_sql_default_connection"))
        mysql = QSqlDatabase::database("qt_sql_default_connection");
      else
        mysql = QSqlDatabase::addDatabase("QSQLITE");
        mysql.setDatabaseName("../myshujuku.db");
        mysql.open();
        //开启事务
        QSqlDatabase::database().transaction();
        //sql
        QSqlQuery query;//不可以做成员,会报错
        //删除原有数据:
        this->exectext=QString("delete from fontandback where rgb_zhig =%1").arg(this->rgb_g_lo);
        query.exec(this->exectext);
        //确定
        QSqlDatabase::database().commit();
        //更新元数据
        this->rgb_g_lo=arg1.toInt();
        //插入数据
        query.exec("create table fontandback(id int primary key, rgbr int, rgbg int, rgbb int, rgb_zhir int, rgb_zhig int, rgb_zhib int, font varchar(20), backfilename varchar(500), logofilename varchar(500))");
        this->exectext=QString("insert into fontandback(id,rgbr,rgbg,rgbb,rgb_zhir,rgb_zhig,rgb_zhib,font,backfilename,logofilename) values(1,%1,%2,%3,%4,%5,%6,'%7','%8','%9')").arg(this->rgb_r).arg(this->rgb_g).arg(this->rgb_b).arg(this->rgb_r_lo).arg(rgb_g_lo).arg(rgb_b_lo).arg(this->qttomp4font).arg(filename_back).arg(filename_logo);
        query.exec(this->exectext);qDebug()<<"fontback"<<this->exectext;
        //确定
        QSqlDatabase::database().commit();
        mysql.close();

    }
}

void fontandback::on_lineEdit_zhi3_textChanged(const QString &arg1)
{

    if(this->yunxinnum<6)
    {
        yunxinnum++;
        qDebug()<<yunxinnum;
        return;
    }
    if(arg1.toInt()<0|arg1.toInt()>255)
    {
        uifont->label_zhungtai->setText("rgb数值在0到255之间");
    }
    else
    {
        QSqlDatabase mysql;
        if(QSqlDatabase::contains("qt_sql_default_connection"))
        mysql = QSqlDatabase::database("qt_sql_default_connection");
      else
        mysql = QSqlDatabase::addDatabase("QSQLITE");
        mysql.setDatabaseName("../myshujuku.db");
        mysql.open();
        //开启事务
        QSqlDatabase::database().transaction();
        //sql
        QSqlQuery query;//不可以做成员,会报错
        //删除原有数据:
        this->exectext=QString("delete from fontandback where rgb_zhib =%1").arg(this->rgb_b_lo);
        query.exec(this->exectext);
        //确定
        QSqlDatabase::database().commit();
        //更新元数据
        this->rgb_b_lo=arg1.toInt();
        //插入数据
        query.exec("create table fontandback(id int primary key, rgbr int, rgbg int, rgbb int, rgb_zhir int, rgb_zhig int, rgb_zhib int, font varchar(20), backfilename varchar(500), logofilename varchar(500))");
        this->exectext=QString("insert into fontandback(id,rgbr,rgbg,rgbb,rgb_zhir,rgb_zhig,rgb_zhib,font,backfilename,logofilename) values(1,%1,%2,%3,%4,%5,%6,'%7','%8','%9')").arg(this->rgb_r).arg(this->rgb_g).arg(this->rgb_b).arg(this->rgb_r_lo).arg(rgb_g_lo).arg(rgb_b_lo).arg(this->qttomp4font).arg(filename_back).arg(filename_logo);
        query.exec(this->exectext);qDebug()<<"fontback"<<this->exectext;
        //确定
        QSqlDatabase::database().commit();
        mysql.close();

    }
}
